package org.iabako.client.ui.form.extranet.signup;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import org.iabako.client.tools.IabakoAsyncCallback;
import org.iabako.client.ui.layout.field.*;
import org.iabako.client.ui.layout.widget.validation.ValidationMessage;
import org.iabako.shared.entity.enumeration.SignUpStep;
import org.iabako.shared.entity.user.User;
import org.iabako.shared.iservice.user.SignUpServiceAsync;
import org.iabako.shared.tools.GenericTools;

/**
 * Created by jose on 08/12/14.
 */
public class SignUpUserForm extends Composite {

    interface MyUiBinder extends UiBinder<HTMLPanel, SignUpUserForm> {
    }

    protected MyUiBinder binderForm = GWT.create(MyUiBinder.class);

    protected final SignUpServiceAsync signUpServiceAsync = SignUpServiceAsync.Util.getInstance();

    @UiField
    LabelText goBack;
    @UiField
    Button goAhead;
    @UiField
    InputText firstName;
    @UiField
    InputText lastName;
    @UiField
    InputTextEmail email;
    @UiField
    InputTextPassword passwordAgain;
    @UiField
    InputTextPassword password;

    SignUpForm signUpForm;

    public SignUpUserForm() {
        initWidget(binderForm.createAndBindUi(this));

        goAhead.getButton().addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                validate();
            }
        });

        goBack.getLabel().addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                ValidationMessage.getFirstFromDOM().clear();
                signUpForm.goTo(SignUpStep.signUpEnterprise);
            }
        });
    }

    public void setSignUpForm(SignUpForm signUpForm) {
        this.signUpForm = signUpForm;
    }

    public boolean validate() {
        if (!uiValidation()) {
            return false;
        }
        User user = getUser();
        if (user == null) {
            return false;
        }

        signUpServiceAsync.saveUser(user, new IabakoAsyncCallback<User>() {
            public void onSuccess(User result) {
                signUpForm.setUser(result);
                signUpForm.setPlainPassword(password.getValue());
                signUpForm.goTo(SignUpStep.signUpEmailValidation);
                super.onSuccess(result);
            }
        });
        return false;

    }

    private boolean uiValidation() {

        ValidationMessage.getFirstFromDOM().clear();

        boolean uiValidation = true;
        if (GenericTools.isEmpty(firstName.getValue())) {
            uiValidation = false;
            firstName.addErrorMessage(firstName, false, "validation_form_mandatory_red_fields_error");
        }
        if (GenericTools.isEmpty(lastName.getValue())) {
            uiValidation = false;
            lastName.addErrorMessage(lastName, false, "validation_form_mandatory_red_fields_error");
        }
        if (GenericTools.isEmpty(email.getValue())) {
            uiValidation = false;
            email.addErrorMessage(email, false, "validation_form_mandatory_red_fields_error");
        }
        if (GenericTools.isEmpty(password.getValue())) {
            uiValidation = false;
            password.addErrorMessage(password, false, "validation_form_mandatory_red_fields_error");
        }

        if (GenericTools.isEmpty(passwordAgain.getValue())) {
            uiValidation = false;
            passwordAgain.addErrorMessage(passwordAgain, false, "validation_form_mandatory_red_fields_error");
        }

        if (uiValidation && !email.isValid()) {
            uiValidation = false;
            email.addErrorMessage(email, false, "validation_form_mandatory_red_fields_error");
        }

        if (uiValidation && !password.isValid()) {
            uiValidation = false;
            password.addErrorMessage(password, false, "validation_form_mandatory_red_fields_error");
        }

        if (uiValidation && !password.getValue().equals(passwordAgain.getValue())) {
            uiValidation = false;
            passwordAgain.addErrorMessage(passwordAgain, true, "passwords_not_equal");
        }

        return uiValidation;

    }

    private User getUser() {
        User user = signUpForm.getUser();
        if (user == null) {
            user = new User();
        }

        user.setFirstName(firstName.getValue());
        user.setLastName(lastName.getValue());
        user.setEmail(email.getValue());
        user.setPassword(GenericTools.getMd5(password.getValue()));
        user.setPrincipal(true);

        user.setEnterprise(signUpForm.getEnterprise());

        //We consider first user's email as the enterprise default email
        user.getEnterprise().setEmail(email.getValue());

        return user;
    }
}
